<template>
  <el-dialog
    :title="!dataForm.id ? '申请休假' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="请假天数" prop="days">
        <el-input-number v-model="dataForm.days" placeholder="天数" :min="1"></el-input-number>
      </el-form-item>
      <el-form-item label="开始日期" prop="startdate">
        <el-date-picker v-model="dataForm.startdate" type="date" value-format="yyyy-MM-dd" placeholder="开始日期" clearable></el-date-picker>
      </el-form-item>
      <el-form-item label="补充说明" prop="remark">
        <el-input type="textarea" :rows="3" v-model="dataForm.remark" placeholder="补充说明"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          days: '',
          startdate: '',
          remark: '',
          procinstanceid: '',
          leaveid: ''
        },
        dataRule: {
          days: [
            { required: true, message: '天数不能为空', trigger: 'blur' }
          ],
          startdate: [
            { required: true, message: '开始日期不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '补充说明不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/flow/leaverequest/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.days = data.data.days
                this.dataForm.startdate = data.data.startdate
                this.dataForm.remark = data.data.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/flow/leaverequest/${!this.dataForm.id ? 'request' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'days': this.dataForm.days,
                'startdate': this.dataForm.startdate,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.leaveid = data.data
                this.$http({
                  url: this.$http.adornUrl('/flow/process/start'),
                  method: 'post',
                  data: this.$http.adornData({
                    'processDefinitionKey': 'Process_leave',
                    'processBusinessKey': this.dataForm.leaveid,
                    'variables': {'days': this.dataForm.days}
                  })
                }).then(({data}) => {
                  if (data && data.code === 0) {
                    this.dataForm.procinstanceid = data.data
                    this.$http({
                      url: this.$http.adornUrl('/flow/leaverequest/setproc'),
                      method: 'get',
                      params: this.$http.adornParams({
                        'leaveid': this.dataForm.leaveid,
                        'proid': this.dataForm.procinstanceid
                      })
                    }).then(({data}) => {
                      if (data && data.code === 0) {
                        this.$message({
                          message: '操作成功',
                          type: 'success',
                          duration: 1500,
                          onClose: () => {
                            this.visible = false
                            this.$emit('refreshDataList')
                          }
                        })
                      } else {
                        this.$message.error(data.msg)
                      }
                    })
                  } else {
                    this.$message.error(data.msg)
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
